package com.cloud.util;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author root
 * @description TODO
 * @date 2020/11/23 14:44
 */
public class JsonUtil {
    /** 
     * @author lc
     * @date 2020/11/23
     * @description 转为json
     */
    public static String toJson(Object obj){
        return JSON.toJSONString(obj);
    }
    /** 
     * @author lc
     * @date 2020/11/23
     * @description 转为对象
     */
    public static <T>T parse(String json,Class<T> cls){
        return JSON.parseObject(json, cls);
    }


    
     /** 
     * @author juhe
     * @date 2020/11/26
     * @description 使用springboot的解析工具
     */
    public static String toObjJson(Object obj){
        try {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * @author lc
     * @date 2020/11/28
     * @description 支持Object
     */
    public static <T>T  parse(Object data, Class<T> cls) {
        return parse(toJson(data), cls);
    }

}